﻿
Option Explicit On


'*******************************************************************
' Program: pkgVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Comments/Base: 

'*******************************************************************

#Region " imports "

Imports System
Imports System.IO
Imports System.IO.Packaging
Imports System.Windows.Forms


Imports System.Xml
Imports System.Linq

Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports <xmlns="http://schemas.microsoft.com/office/visio/2012/main">

Imports pkgVisio.clsVisPackage

Imports System.Text
Imports System.Xml.Schema
Imports System.Xml.XmlReader
Imports pkgVisio.clsStructures

#End Region



Public Class frmValidation


#Region " Properties and Declarations "




    '
    ' what is the latest document
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property

    ' the local class definitions
    Private m_visPackage As clsVisPackage
    Public Property visPackage As clsVisPackage
        Get
            visPackage = Me.m_visPackage
        End Get
        Set(value As clsVisPackage)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "visPackage is Null.")
            End If
            Me.m_visPackage = value
        End Set
    End Property

    Private m_visValidation As clsValidation
    Public Property visValidation As clsValidation
        Get
            visValidation = Me.m_visValidation
        End Get
        Set(value As clsValidation)
            Me.m_visValidation = value
        End Set
    End Property

    Private m_fileCurrent As String
    Public Property fileCurrent As String
        Get
            fileCurrent = m_fileCurrent
        End Get
        Set(value As String)
            m_fileCurrent = value
            If ((value Is Nothing) Or (value = "")) Then
                Throw New System.ArgumentNullException("Value", _
                    "fileCurrent is Null.")
            End If
        End Set
    End Property

    Private m_colPkgPartInfo As Collection(Of pkgpartInfo)
    Public Property colPkgPartInfo As Collection(Of pkgpartInfo)
        Get
            colPkgPartInfo = m_colPkgPartInfo
        End Get
        Set(value As Collection(Of pkgpartInfo))
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "colPkgPartsInfo is Null.")
            End If
            m_colPkgPartInfo = value
        End Set
    End Property

#End Region


#Region " generic methods "


    '
    '
    '
    Public Sub initControls()

        Me.lvRuleSets.Clear()
        Me.lvRules.Clear()
        Me.lvIssues.Clear()
        Me.lvIssueTarget.Clear()
        Me.lvRuleInfo.Clear()

        Me.txtRuleCategory.Text = ""
        Me.txtRuleDescription.Text = ""
        Me.txtRuleFilter.Text = ""
        Me.txtRuleId.Text = ""
        Me.txtRuleNameU.Text = ""
        Me.txtRuleSetDescription.Text = ""
        Me.txtRuleSetId.Text = ""
        Me.txtRuleSetNameU.Text = ""
        ' Me.txtRuleTarget.Text = ""
        Me.txtRuleTest.Text = ""

    End Sub

    '
    '
    '
    Public Sub initClasses()
        Try

            Dim frmParent As frmMain = Nothing
            If Me.Owner IsNot Nothing Then
                ' should be frmMain
                frmParent = Me.Owner
                Me.fileCurrent = frmParent.visPackage.fileCurrent
                Me.visPackage = frmParent.visPackage
                Me.visPackage.loadPkgAndParts()
                Me.visPackage.colPkgPartInfo = Me.colPkgPartInfo
            End If

            ' build the primary dictionaries for page and shape information
            If Me.visPackage.dictPageShape.Count = 0 Then
                Me.visPackage.buildPageAndShapeDictionaries()
                If frmParent IsNot Nothing Then
                    frmParent.visPackage.dictPagesID = Me.visPackage.dictPagesID
                    frmParent.visPackage.dictPageShape = Me.visPackage.dictPageShape
                End If
            End If

            If visValidation IsNot Nothing Then
                Me.m_visValidation = Nothing
            End If
            Me.visValidation = New clsValidation

            If Me.visValidation.m_visPackage IsNot Nothing Then
                Me.visValidation.m_visPackage = Nothing
            End If
            Me.visValidation.visPackage = Me.visPackage



        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub loadRuleFormInfo _
        (ByVal ruleSource As XElement)

        Try


            Dim lngRuleId As Long = -1
            Dim strRuleNameU As String = "NoRuleNameU"
            Dim strRuleName As String = "NoRuletName"
            Dim strRuleDescription As String = "NoRuleDescription"
            Dim strRuleCategory As String = "NoRuleCategory"
            Dim lngRuleTarget As Long = -1
            Dim strRuleFilter As String = "NoRuleFilter"
            Dim strRuleTest As String = "NoRuleTest"

            Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In ruleSource.Attributes() _
                    Select att

            For Each att As XAttribute In listOfAttributes
                ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                Select Case LCase(att.Name.LocalName)
                    Case "id"
                        lngRuleId = CLng(att.Value)
                    Case "nameu"
                        strRuleNameU = CStr(att.Value)
                    Case "description"
                        strRuleDescription = CStr(att.Value)
                    Case "category"
                        strRuleCategory = CStr(att.Value)
                    Case "target"
                        lngRuleTarget = CLng(att.Value)
                End Select
            Next att


            Dim listOfElements As IEnumerable(Of XElement) = _
                    From elemData In ruleSource.Elements() _
                    Select elemData

            If listOfElements.Count > 0 Then
                For Each elem As XElement In listOfElements
                    ' Console.WriteLine("{0}={1}", elem.Name.LocalName, elem.Value)
                    Select Case LCase(elem.Name.LocalName)
                        Case "rulefilter"
                            strRuleFilter = CStr(elem.Value)
                        Case "ruletest"
                            strRuleTest = CStr(elem.Value)
                    End Select
                Next elem

            End If

            Me.txtRuleId.Text = CStr(lngRuleId)
            Me.txtRuleNameU.Text = strRuleNameU
            Me.txtRuleDescription.Text = strRuleDescription
            Me.txtRuleCategory.Text = strRuleCategory
            ' Me.txtRuleTarget.Text = CStr(lngRuleTarget)
            Me.txtRuleFilter.Text = CStr(strRuleFilter)
            Me.txtRuleTest.Text = CStr(strRuleTest)

        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

#End Region



    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub

    '
    '
    '
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.initControls()

        Me.m_visValidation = Nothing
        Me.visValidation = New clsValidation

        Me.m_visPackage = Nothing
        Me.visPackage = New clsVisPackage


    End Sub

    '
    '
    '
    Private Sub frmValidation_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown

        Try


            initControls()

            initClasses()

            Dim colPkgParts As Collection(Of pkgpartInfo) = New Collection(Of pkgpartInfo)
            colPkgParts.Clear()

            colPkgParts = Me.visPackage.getPkgPartsByContent(pkgVisio.clsVisPackage.contXmlValidation)
            If colPkgParts.Count = 1 _
                And Me.visPackage.colPkgPartInfo.Count <> 0 Then
                Dim pkgInfo As pkgpartInfo = colPkgParts(0)
                Me.visValidation.xDoc = Me.visPackage.getPackagePartAsXDoc(Me.visPackage.fileCurrent, pkgInfo)

                ' get the issues documented
                Me.visValidation.getRuleIssues(Me.lvIssues)
                If Me.lvIssues.Items.Count > 0 Then
                    Dim issueItem As ListViewItem = lvIssues.Items(0)
                    ' query the rules
                    Dim qryIssue = From issueSel In Me.visValidation.xDoc...<Issue> _
                            Where issueSel.@ID = issueItem.SubItems(0).Text
                    If qryIssue.Count = 1 Then
                        Me.visValidation.getRuleInfo(qryIssue.First, Me.lvRuleInfo)
                        Me.visValidation.getIssueTargetInfo(qryIssue.First, Me.lvIssueTarget)
                    End If ' test that only one issue was found
                End If ' test to see if issues were detected

                ' get the rulesets
                Me.visValidation.getRuleSets()
                '
                '
                Me.visValidation.loadRuleSetLvControl(Me.lvRuleSets)
                Dim rulesetItems As ListView.SelectedListViewItemCollection = lvRuleSets.SelectedItems
                Dim rulesetItem As ListViewItem
                rulesetItem = rulesetItems(0)
                Me.txtRuleSetId.Text = rulesetItem.SubItems(0).Text
                Me.txtRuleSetNameU.Text = rulesetItem.SubItems(1).Text
                Me.txtRuleSetDescription.Text = rulesetItem.SubItems(2).Text

                ' since this is the first load the initial ruleset should also be built
                Me.visValidation.loadRulesLvControl(Me.lvRules)
                If lvRules.Items.Count > 0 Then
                    lvRules.Focus()
                    lvRules.Items(0).Selected = True
                End If

            End If ' test for validation packagepart

        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub lvRules_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvRules.SelectedIndexChanged

        Try


            Dim ruleItems As ListView.SelectedListViewItemCollection = lvRules.SelectedItems
            Dim ruleItem As ListViewItem


            If ruleItems.Count = 1 Then

                ruleItem = ruleItems(0)

                ' query the rules
                Dim qryRulesets = From rulesetSel In Me.visValidation.xDoc...<RuleSet> _
                        Where rulesetSel.@ID = Me.txtRuleSetId.Text

                If qryRulesets.Count = 1 Then

                    ' query the rules
                    Dim qryRules = From ruleSel In qryRulesets...<Rule> _
                            Where ruleSel.@ID = ruleItem.SubItems(0).Text

                    If qryRules.Count = 1 Then
                        loadRuleFormInfo(qryRules.First)
                    End If ' test that only one ruleset was found

                End If ' test that only one ruleset was found
            End If ' test for something selected


        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Private Sub lvRuleSets_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvRuleSets.SelectedIndexChanged

        Try


            Dim rulesetItems As ListView.SelectedListViewItemCollection = lvRuleSets.SelectedItems
            Dim rulesetItem As ListViewItem


            If 0 < rulesetItems.Count Then

                rulesetItem = rulesetItems(0)
                Me.txtRuleSetId.Text = rulesetItem.SubItems(0).Text
                Me.txtRuleSetNameU.Text = rulesetItem.SubItems(1).Text
                Me.txtRuleSetDescription.Text = rulesetItem.SubItems(2).Text

                ' query the rules
                Dim qryRulesets = From rulesetSel In Me.visValidation.xDoc...<RuleSet> _
                        Where rulesetSel.@ID = rulesetItem.SubItems(0).Text

                If qryRulesets.Count = 1 Then
                    Me.visValidation.getRules(qryRulesets.First())
                    Me.visValidation.loadRulesLvControl(Me.lvRules)
                End If ' test that only one ruleset was found

            End If ' test for something selected



        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Private Sub lvIssues_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvIssues.SelectedIndexChanged

        Dim issueItems As ListView.SelectedListViewItemCollection = lvIssues.SelectedItems
        Dim issueItem As ListViewItem

        If 0 < issueItems.Count Then

            issueItem = issueItems(0)

            ' query the rules
            Dim qryIssue = From issueSel In Me.visValidation.xDoc...<Issue> _
                    Where issueSel.@ID = issueItem.SubItems(0).Text

            If qryIssue.Count = 1 Then
                Me.visValidation.getRuleInfo(qryIssue.First, Me.lvRuleInfo)
                Me.visValidation.getIssueTargetInfo(qryIssue.First, Me.lvIssueTarget)
            End If ' test that only one issue was found

        End If ' test for something selected


    End Sub

    '
    ' if a rule info object is selected pre-load the ruleset/rules page and then move to that page
    '
    Private Sub lvRuleInfo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvRuleInfo.SelectedIndexChanged

        Try


            Dim infoItems As ListView.SelectedListViewItemCollection = lvRuleInfo.SelectedItems
            Dim infoItem As ListViewItem

            If 0 < infoItems.Count Then

                infoItem = infoItems(0)

                Dim lngRuleset As Long = CLng(infoItem.SubItems(1).Text)
                Dim lngRule As Long = CLng(infoItem.SubItems(3).Text)

                Dim rulesetItem As ListViewItem
                Me.lvRuleSets.SelectedItems.Clear()
                Me.lvRuleSets.SelectedIndices.Clear()
                For Each rulesetItem In Me.lvRuleSets.Items
                    If rulesetItem.SubItems(0).Text = infoItem.SubItems(1).Text Then
                        Me.txtRuleSetId.Text = rulesetItem.SubItems(0).Text
                        Me.txtRuleSetNameU.Text = rulesetItem.SubItems(1).Text
                        Me.txtRuleSetDescription.Text = rulesetItem.SubItems(2).Text

                        ' query the rules
                        Dim qryRulesets = From rulesetSel In Me.visValidation.xDoc...<RuleSet> _
                                Where rulesetSel.@ID = infoItem.SubItems(1).Text

                        If qryRulesets.Count = 1 Then
                            Me.visValidation.getRules(qryRulesets.First())
                            Me.visValidation.loadRulesLvControl(Me.lvRules)
                        End If ' test that only one ruleset was found
                        Exit For
                    End If
                Next rulesetItem

                Dim ruleItem As ListViewItem
                Me.lvRuleSets.SelectedIndices.Clear()
                Me.lvRuleSets.SelectedItems.Clear()
                For Each ruleItem In Me.lvRules.Items
                    If ruleItem.SubItems(0).Text = infoItem.SubItems(3).Text Then

                        ' query the rules
                        Dim qryRulesets = From rulesetSel In Me.visValidation.xDoc...<RuleSet> _
                                Where rulesetSel.@ID = infoItem.SubItems(1).Text

                        If qryRulesets.Count = 1 Then

                            ' query the rules
                            Dim qryRules = From ruleSel In qryRulesets...<Rule> _
                                    Where ruleSel.@ID = infoItem.SubItems(3).Text

                            If qryRules.Count = 1 Then
                                loadRuleFormInfo(qryRules.First)
                            End If ' test that only one ruleset was found

                        End If ' test that only one ruleset was found
                        Exit For
                    End If
                Next ruleItem

                Me.TabControl1.SelectedTab = Me.tabRulesets

            End If

        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


End Class